<?php

function load_enterprise_information2_fields_ver2()
{
	$file_path = drupal_get_path("module", "enterprise_information2")."/schema.data";
	$fp = fopen($file_path, "r");
	$fields = array();
	while (!feof($fp))
	{
	    $line=fgets($fp);
		$line = chop($line);
		$sep = explode("|", $line);
		$fields[] = array("engname"=>$sep[0],"chiname"=>$sep[1],"type"=>$sep[2]);
	}
	fclose($fp);
	return $fields;
}
function form_page_html($form_name)
{
	$html = array("#markup" => "");
	$d_form = drupal_get_form($form_name);
	$html["#markup"] .= drupal_render($d_form);
	return $html;
}
function get_info_by_id($id)
{
/*
 * 读取数据库，返回$id对应的数据库的记录，以关联数组形式存储。
 */
	$query = "SELECT * FROM {nishejijin} WHERE id = ?";
	$query_result = db_query($query,array($id));
	$result_values = $query_result->fetchAssoc();
	return $result_values;
}
function get_info_by_user_id($user_id)
{
/*
 * 读取数据库，返回$user_id对应的数据库的记录，以关联数组形式存储。
 */
	$query = "SELECT * FROM {nishejijin} WHERE user_id = ?";
	$query_result = db_query($query,array($user_id));
	$result_values = $query_result->fetchAssoc();
	return $result_values;
}
function get_part_by_state($state)
{
/*
 * 读取数据库，返回$state对应的数据库的记录的id，以关联数组形式存储。
 */
	$query = "SELECT id,nishejijinmingcheng,jijinguimo,zhuyaotouzilingyu,guanlijigoumingcheng,lianxiren,lianxidianhua FROM {nishejijin} WHERE state = ?";
	$query_result = db_query($query,array($state));
	$result_values = array();
	while($newitem = $query_result->fetchAssoc()) {
		$result_values[] = $newitem;
	}
	return $result_values;
}
function get_ids_by_state($state)
{
/*
 * 读取数据库，返回$state对应的数据库的记录的id，以关联数组形式存储。
 */
	$query = "SELECT id FROM {nishejijin} WHERE state = ?";
	$query_result = db_query($query,array($state));
	$result_values = array();
	while($newitem = $query_result->fetchAssoc()) {
		$result_values[] = $newitem["id"];
	}
	return $result_values;
}
function get_all_by_state($state)
{
/*
 * 读取数据库，返回$state对应的数据库的记录的id，以关联数组形式存储。
 */
	$query = "SELECT * FROM {nishejijin} WHERE state = ?";
	$query_result = db_query($query,array($state));
	$result_values = array();
	while($newitem = $query_result->fetchAssoc()) {
		$result_values[] = $newitem;
	}
	return $result_values;
}
function get_state_by_id($id)
{
	$query = "SELECT * FROM {nishejijin} WHERE id = ?";
	$query_result = db_query($query,array($id));
	$result_values = array();
	while($newitem = $query_result->fetchAssoc()) {
		$result_values[] = $newitem;
	}
	return $result_values[0]["state"];
}
function set_state_by_id($state,$id)
{
	$query = db_update("nishejijin")->fields(array("state"=>$state))->condition("id",$id);
	$result_values = $query->execute();
	return $result_values;
}

function parse_chuzilaiyuan($line)
{
/*
 * 将出资来源解析为array()
 */
	$lines = explode("\n", $line);
	$chuzilaiyuan = array("data"=>array(),"amount"=>0);
	foreach ($lines as $value) {
		$dataline = explode(" ", $value);
		$dataline1 = array();
		foreach($dataline as $item)
			if($item != "") $dataline1[] = $item;
		$dataline = $dataline1;
		if(count($dataline) != 2)
			continue;
		$chuzilaiyuan["data"][] = array("chuziren"=>$dataline[0],"chuzie"=>$dataline[1]);
		$chuzilaiyuan["amount"] += $dataline[1];
	}
	if($chuzilaiyuan["amount"] <= 0){
		$chuzilaiyuan["amount"] = 1;
	}
	foreach ($chuzilaiyuan["data"] as $key => $value) {
		$chuzilaiyuan["data"][$key]["chuzibili"] = number_format($value["chuzie"] / $chuzilaiyuan["amount"] *100,2 )."%";
	}
	return $chuzilaiyuan;
}
function chuzilaiyuan_table_make($default_line)
{
/* 
 * 出资来源表的制作
 */
	$default_values = array();
	if($default_line != NULL){
		$default_values = parse_chuzilaiyuan($default_line);
	}else{
		$default_values = parse_chuzilaiyuan("青岛市市级创业投资引导基金管理中心 0");
	}
	$table_html = "<table><tbody id='tbodyadd'><tr><th>出资人</th><th>出资额(万元)</th></tr>";
	for ($i=0; $i < 5; $i++) { 
		if($i < count($default_values['data'])){
			$table_html .= "<tr><td><input type='text' name='chuzilaiyuan".$i."A' value='".$default_values['data'][$i]['chuziren']."'></td>".
			"<td><input type='text' name='chuzilaiyuan".$i."B' value='".$default_values['data'][$i]['chuzie']."'></td></tr>";
		}else{
			$table_html .= "<tr><td><input type='text' name='chuzilaiyuan".$i."A'></td>".
			"<td><input type='text' name='chuzilaiyuan".$i."B'></td></tr>";
		}
	}
	$table_html .= "</tbody></table><span id='addtr'>增加出资人</span>";
	return $table_html;
}
function extract_chuzilaiyuan($values)
{
/*
 * 从提交的values中提取出出资来源
 */	
	$i = 0;
	$chuzilaiyuan_str = "";
	while(TRUE){
		if(array_key_exists("chuzilaiyuan".$i."A", $values) && $values["chuzilaiyuan".$i."A"] != ""){
			$chuzie = 0;
			if ($values["chuzilaiyuan".$i."B"] != ""){
				$chuzie = floatval($values["chuzilaiyuan".$i."B"]);
			}
			$chuzilaiyuan_str .= $values["chuzilaiyuan".$i."A"]." ".$chuzie."\n";
		}
		else {
			break;
		}
		$i += 1;
	}
	return $chuzilaiyuan_str;
}
function declare_check_n_go($goingto)
{
	global $user;
	$field_values = get_info_by_user_id($user->uid);
	if($field_values == FALSE){
		$act = "create";
		if ($act != $goingto) drupal_goto("enterprise_information2/agreement");
	}elseif($field_values["state"]==0){
		$act = "edit";
		if ($act != $goingto) drupal_goto("enterprise_information2/edit");
	}else{
		$act = "view";
		if ($act != $goingto) drupal_goto("enterprise_information2/view");
	}
	return;
}
function enterprise_information2_agreement_page()
{
	declare_check_n_go('create');
	return form_page_html('enterprise_information2_agreement_form');
}
function enterprise_information2_agreement_form($form)
{
	$form['information'] = array(
		'#type' => 'markup',
		'#markup' => '<ol><li>引导基金参股基金注册在青岛</li><li>从公示结束到首轮注资原则上不超过6个月，否则本次申报失效</li>
					<li>鉴于引导基金财政资金性质，如需相关审核审计程序，应予以配合。</li><li>基金投资人税收需由基金公司代扣代缴至基金注册地。</li>
					<li>基金不得从事以下业务：
					<ul><li>A)投资于已上市企业，但是所投资的未上市企业上市后，参股基金所持股份未转让及其配售部分除外;</li>
					<li>B)从事担保、抵押、委托贷款、房地产（包括购买自用房地产）等业务；</li>
					<li>C)投资其他创业投资基金或投资性企业；</li>
					<li>D)投资于股票、期货、企业债券、信托产品、理财产品、保险计划及其他金融衍生品；</li>
					<li>E)向任何第三人提供赞助、捐赠等；</li>
					<li>F)吸收或变相吸收存款，或向任何第三人提供贷款和资金拆借；</li>
					<li>G)投资于合伙或有限合伙企业及其他承担无限连带责任的对外投资；</li>
					<li>H)以发行信托或集合理财产品的形式募集资金；</li>
					<li>I)存续期内，投资回收资金再用于对外投资；</li>
					<li>J) 使用贷款进行投资；</li>
					<li>K) 投资于违背国家产业政策或青岛城市功能定位的项目；</li>
					<li>L）其他法律法规和政策禁止从事的业务。</li></ul></li>
					<li>基金认可并遵守创业投资相关法规条例要求。</li></ol>',
	);
	$form['agree'] = array(
		'#type' => 'checkbox',
		'#title' => '同意以上条款',
	);
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('提交'),
	);
	return $form;
}
function enterprise_information2_agreement_form_submit($form, &$form_state)
{
	if($form_state['values']['agree'] == 1)
		$form_state['redirect'] = 'enterprise_information2/create';
	else
		$form_state['redirect'] = 'enterprise_information2/agreement';
}
function enterprise_information2_create_page()
{
	return form_page_html("enterprise_information2_create_form");
}
function enterprise_information2_create_form($form)
{
	$fields = load_enterprise_information2_fields_ver2();
	$i = 0;
	foreach ($fields as $value) {
		if($value["type"] == "str"){
			$form[$value["engname"]] = array(
			"#type"=>"textfield",
			"#prefix"=>"<tr><td>".$value["chiname"]."</td><td>",
			"#suffix"=>"</td></tr>");
		}
		else if($value["type"] == "txt"){
			$form[$value["engname"]] = array(
				"#type"=>"textarea",
				"#prefix"=>"<tr><td>".$value["chiname"]."</td><td>",
				"#suffix"=>"</td></tr>"
			);
			if($value["engname"]=="pingshenyijian"){
				$form[$value["engname"]]["#disabled"]=TRUE;
			}
			elseif($value["engname"]=="chuzilaiyuan"){
				$form[$value["engname"]]["#prefix"] = "<tr><td>出资来源</td><td>";
				$form[$value["engname"]]["#type"] = "markup";
				$form[$value["engname"]]["#markup"] = chuzilaiyuan_table_make(NULL);
			}
		}
		else if($value["type"] == "num" || $value["type"] == "int") {
			$form[$value["engname"]] = array(
				"#type"=>"textfield",
				"#default_value"=>0,
				"#prefix"=>"<tr><td>".$value["chiname"]."</td><td>",
				"#suffix"=>"</td></tr>"
			);
		}
		if ($i == 0)
			$form[$value["engname"]]["#prefix"] = "<table style=\"width:60%\">".$form[$value["engname"]]["#prefix"];
		if ($i == count($fields)-1)
			$form[$value["engname"]]["#suffix"] = $form[$value["engname"]]["#suffix"]."</table>";
		$i += 1;
	}
	$form['upload'] = array(
	'#type' => 'managed_file',
	'#title' => '附件上传',
	'#upload_location' => 'public://my_uploads',
	'#upload_validators' => array(
		'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
		'file_validate_size' => array(50*1024*1024)),
	);
	$form['file_name'] = array(
	'#title' => t('显示文件名：'),
	'#type' => 'textfield',
	'#default_value' =>"基金申请报告",
	);
	$form["save"] = array(
	"#type" => "submit",
	'#value' => t("暂存"),
	'#submit' => array('enterprise_information2_create_form_save'),
	);
	$form["submit"] = array(
	"#type" => "submit",
	'#value' => t("提交"),
	);
	$path = drupal_get_path('module', 'enterprise_information2');
	$form['#attached'] = array(
	'js' => array
		(
			'type' => 'file',
			'data' => $path . '/tb.js',
		),
	);
	return $form;
}
function enterprise_information2_create_form_validate($form,&$form_state)
{
	$fields = load_enterprise_information2_fields_ver2();
	$values = $form_state["values"];
	foreach ($fields as $field) {
		if($field["type"]=="int"){
			if(!($values[$field["engname"]]>=0) || !(is_int($values[$field["engname"]]+1))){
				form_set_error($field["engname"],"请输入正整数！");
			}
		}elseif($field["type"]=="num"){
			if(!($values[$field["engname"]]>=0) || !(is_numeric($values[$field["engname"]]))){
				form_set_error($field["engname"],"请输入正数！");
			}
		}
	}
}
function enterprise_information2_create_form_submit($form,&$form_state)
{
	$values = $form_state["values"];
	$fields = load_enterprise_information2_fields_ver2();
	$fid = $form_state['values']['upload']; 
	$file = file_load($fid);  
	$filename = $values['file_name'];
	if($filename == "")
		$filename = "基金申请报告";
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
	    $fid = $file->fid;
		$extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
		}
		else {
			form_set_error('upload',$extension_errors[0]);
		}
	}
	$values["chuzilaiyuan"] = extract_chuzilaiyuan($form_state['input']);
	global $user;
	$db_fields = array(
		"state" => 1,
		"user_id" => $user->uid,
		"timestamp" => $_SERVER["REQUEST_TIME"],
	);
	foreach ($fields as $i) {
		$db_fields[$i["engname"]]= $values[$i["engname"]];
	}
	if ($file != NULL) {
		$db_fields["file_name"] = $filename;
		$db_fields["url"] = $saveurl;
		$db_fields["file_managed_id"] = $fid;
	}
	$last_id = db_insert('nishejijin')->fields($db_fields)->execute();
	if($last_id)
		$form_state["redirect"] = "enterprise_information2/view";
	else {
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
	}
}
function enterprise_information2_create_form_save($form,&$form_state)
{
	$values = $form_state["values"];
	$fields = load_enterprise_information2_fields_ver2();
	$fid = $form_state['values']['upload']; 
	$file = file_load($fid);  
	$filename = $values['file_name'];
	if($filename == "")
		$filename = "基金申请报告";
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
	    $fid = $file->fid;
		$extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
		}
		else {
			form_set_error('upload',$extension_errors[0]);
		}
	}
	$values["chuzilaiyuan"] = extract_chuzilaiyuan($form_state['input']);
	global $user;
	$db_fields = array(
		"state" => 0,
		"user_id" => $user->uid,
		"timestamp" => $_SERVER["REQUEST_TIME"],
	);
	foreach ($fields as $i) {
		$db_fields[$i["engname"]]= $values[$i["engname"]];
	}
	if ($file != NULL) {
		$db_fields["file_name"] = $filename;
		$db_fields["url"] = $saveurl;
		$db_fields["file_managed_id"] = $fid;
	}
	$last_id = db_insert('nishejijin')->fields($db_fields)->execute();
	if($last_id)
		$form_state["redirect"] = "enterprise_information2/view";
	else {
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
	}
}
function enterprise_information2_edit_page()
{
	declare_check_n_go("edit");
	return form_page_html("enterprise_information2_edit_form");
}
function enterprise_information2_edit_form($form)
{
	global $user;
	$field_values = get_info_by_user_id($user->uid);
	$fields = load_enterprise_information2_fields_ver2();
	$i = 0;
	foreach ($fields as $value) {
		if($value["type"] != "txt"){
			$form[$value["engname"]] = array(
				"#type"=>"textfield",
				"#prefix"=>"<tr><td>".$value["chiname"]."</td><td>",
				"#suffix"=>"</td></tr>",
				"#default_value"=>$field_values[$value["engname"]]
			);
		}
		else {
			$form[$value["engname"]] = array(
				"#type"=>"textarea",
				"#prefix"=>"<tr><td>".$value["chiname"]."</td><td>",
				"#suffix"=>"</td></tr>",
				"#default_value"=>$field_values[$value["engname"]]
			);
			if($value["engname"]=="pingshenyijian"){
				$form[$value["engname"]]["#disabled"]=TRUE;
			}elseif($value["engname"]=="chuzilaiyuan"){
				$form[$value["engname"]]["#prefix"] = "<tr><td>出资来源</td><td>";
				$form[$value["engname"]]["#type"] = "markup";
				$form[$value["engname"]]["#markup"] = chuzilaiyuan_table_make($field_values[$value["engname"]]);
			}
		}
		if ($i == 0)
			$form[$value["engname"]]["#prefix"] = "<table style=\"width:60%\">".$form[$value["engname"]]["#prefix"];
		if ($i == count($fields)-1)
			$form[$value["engname"]]["#suffix"] = $form[$value["engname"]]["#suffix"]."</table>";
		$i += 1;
	}
	$form['upload'] = array(
	'#type' => 'managed_file',
	'#title' => '附件上传',
	'#upload_location' => 'public://my_uploads',
	'#upload_validators' => array(
		'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
		'file_validate_size' => array(50*1024*1024)),
	);
	$form['file_name'] = array(
	'#title' => t('显示文件名：'),
	'#type' => 'textfield',
	'#default_value' =>"基金申请报告",
	);
	$form['bid'] = array(
	'#type' => 'hidden',
	"#value" => $field_values["id"],
	);
	$form["save"] = array(
	"#type" => "submit",
	'#value' => t("暂存"),
	'#submit' => array("enterprise_information2_edit_form_save"),
	);
	$form["submit"] = array(
	"#type" => "submit",
	'#value' => t("保存并提交(提交后不能再进行修改)"),
	);
	$path = drupal_get_path('module', 'enterprise_information2');
	$form['#attached'] = array(
	'js' => array
		(
			'type' => 'file',
			'data' => $path . '/tb.js',
		),
	);
	return $form;
}
function enterprise_information2_edit_form_validate($form,&$form_state)
{
	$fields = load_enterprise_information2_fields_ver2();
	$values = $form_state["values"];
	foreach ($fields as $field) {
		if($field["type"]=="int"){
			if(!($values[$field["engname"]]>=0) || !(is_int($values[$field["engname"]]+1))){
				form_set_error($field["engname"],"请输入正整数！");
			}
		}elseif($field["type"]=="num"){
			if(!($values[$field["engname"]]>=0) || !(is_numeric($values[$field["engname"]]))){
				form_set_error($field["engname"],"请输入正数！");
			}
		}
	}
}
function enterprise_information2_edit_form_submit($form,&$form_state)
{
	$values = $form_state["values"];
	$fields = load_enterprise_information2_fields_ver2();
	$fid = $form_state['values']['upload']; 
	$file = file_load($fid);  
	$filename = $values['file_name'];
	if($filename == "")
		$filename = "基金申请报告";
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
	    $fid = $file->fid;
		$extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
		}
		else {
			form_set_error('upload',$extension_errors[0]);
		}
	}
	$values["chuzilaiyuan"] = extract_chuzilaiyuan($form_state['input']);
	global $user;
	$db_fields = array(
		"state" => 1,
		"user_id" => $user->uid,
		"timestamp" => $_SERVER["REQUEST_TIME"],
	);
	foreach ($fields as $i) {
		$db_fields[$i["engname"]]= $values[$i["engname"]];
	}
	if ($file != NULL) {
		$db_fields["file_name"] = $filename;
		$db_fields["url"] = $saveurl;
		$db_fields["file_managed_id"] = $fid;
	}
	$last_id = db_update('nishejijin')->fields($db_fields)->condition("id",$values["bid"])->execute();
	if($last_id)
		$form_state["redirect"] = "enterprise_information2/view";
	else {
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
	}
}
function enterprise_information2_edit_form_save($form,&$form_state)
{
	$values = $form_state["values"];
	$fields = load_enterprise_information2_fields_ver2();
	$fid = $form_state['values']['upload']; 
	$file = file_load($fid);  
	$filename = $values['file_name'];
	if($filename == "")
		$filename = "基金申请报告";
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
	    $fid = $file->fid;
		$extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
		}
		else {
			form_set_error('upload',$extension_errors[0]);
		}
	}
	$values["chuzilaiyuan"] = extract_chuzilaiyuan($form_state['input']);
	global $user;
	$db_fields = array(
		"state" => 0,
		"user_id" => $user->uid,
		"timestamp" => $_SERVER["REQUEST_TIME"],
	);
	foreach ($fields as $i) {
		$db_fields[$i["engname"]]= $values[$i["engname"]];
	}
	if ($file != NULL) {
		$db_fields["file_name"] = $filename;
		$db_fields["url"] = $saveurl;
		$db_fields["file_managed_id"] = $fid;
	}
	$last_id = db_update('nishejijin')->fields($db_fields)->condition("id",$values["bid"])->execute();
	if($last_id)
		$form_state["redirect"] = "enterprise_information2/view";
	else {
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
	}
}
function enterprise_information2_view_page()
{
	declare_check_n_go("view");
	$html = array("#markup"=>"");
	$fields = load_enterprise_information2_fields_ver2();
	global $user;
	$field_values = get_info_by_user_id($user->uid);
	$html["#markup"] .= view_by_table($fields, $field_values, "enterprise");
	return $html;
}
function enterprise_information2_viewbyid_page($id)
{
	$html = array("#markup"=>l('删除该基金信息','enterprise_information2/deletebyid/'.$id));
	$fields = load_enterprise_information2_fields_ver2();
	$field_values = get_info_by_id($id);
	$html["#markup"] .= view_by_table($fields, $field_values, "manager");
	return $html;
}
function enterprise_information2_deletebyid_page($id)
{
    db_delete('nishejijin')
        ->condition('id' ,$id)
        ->execute();
    drupal_goto('announce/managelist');
}

function enterprise_information2_examine_page($id)
{
	$html = array("#markup"=>"");
	$fields = load_enterprise_information2_fields_ver2();
	$field_values = get_info_by_id($id);
	$html["#markup"] .= view_by_table($fields, $field_values, "manager");
	$d_form = drupal_get_form("enterprise_information2_examine_form",$id);
	$html["#markup"] .= drupal_render($d_form);
	return $html;
}
function enterprise_information2_examine_form($form,&$form_state,$id)
{
	$form["pingshenyijian"] = array(
		"#type"=>"textarea",
		"#title"=>"初审意见",
	);
	$form["bid"] = array(
		"#type"=>"hidden",
		"#value"=>$id,
	);
	$form["reject"] = array(
		"#type"=>"submit",
		"#value"=>t("发回修改"),
		"#submit"=>array("enterprise_information2_examine_form_reject"),
	);
	$form["pass"] = array(
		"#type"=>"submit",
		"#value"=>t("进入专家评审"),
	);
	$form["trash"] = array(
		"#type"=>"submit",
		"#value"=>t("停止合作"),
		"#submit"=>array("enterprise_information2_examine_form_trash"),
	);
	return $form;
}
function enterprise_information2_examine_form_reject($form,&$form_state)
{
	$values = $form_state["values"];
	$last_id = db_update('nishejijin')->fields(array(
	"pingshenyijian"=>$values["pingshenyijian"],
	"state"=>0))
	->condition("id",$values["bid"])
	->execute();
	if($last_id)
		$form_state["redirect"] = "enterprise_information2/list";
	else {
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
	}
}
function enterprise_information2_examine_form_submit($form,&$form_state)
{
	$values = $form_state["values"];
	$last_id = db_update('nishejijin')->fields(array(
	"pingshenyijian"=>$values["pingshenyijian"],
	"state"=>3))
	->condition("id",$values["bid"])
	->execute();
	if($last_id)
		$form_state["redirect"] = "enterprise_information2/list";
	else {
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
	}
}
function enterprise_information2_examine_form_trash($form,&$form_state)
{
	$values = $form_state["values"];
	$last_id = db_update('nishejijin')->fields(array(
	"pingshenyijian"=>$values["pingshenyijian"],
	"state"=>2))
	->condition("id",$values["bid"])
	->execute();
	if($last_id)
		$form_state["redirect"] = "enterprise_information2/list";
	else {
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');	    
	}
}
function enterprise_info_id($id)
{
	$fields = load_enterprise_information2_fields_ver2();
	$field_values = get_info_by_id($id);
	return view_by_table($fields, $field_values, "expert");
}
function view_by_table($fields,$field_values,$role)
{
	$html = "";
	$html .= "<table style=\"width:60%\">";
	for ($i=0; $i < 8 ; $i++) {
        if ($fields[$i]["chiname"] == "基金规模（万元）")
            $html .= "<tr><td style=\"width:30%\">".$fields[$i]["chiname"]."</td><td colspan=\"3\">".(int)$field_values[$fields[$i]["engname"]]."</td></tr>";
        else
		    $html .= "<tr><td style=\"width:30%\">".$fields[$i]["chiname"]."</td><td colspan=\"3\">".$field_values[$fields[$i]["engname"]]."</td></tr>";
	}
	$chuzilaiyuan = parse_chuzilaiyuan($field_values["chuzilaiyuan"]);
	if ($chuzilaiyuan != FALSE){
		$html .= "<tr><td rowspan=\"".(count($chuzilaiyuan["data"])+2)."\">出资来源</td><td>出资人</td><td>出资额(万元)</td><td>出资比例</td></tr>";
		for ($i=0; $i < count($chuzilaiyuan["data"]); $i++) { 
			$html .= "<tr><td>".$chuzilaiyuan["data"][$i]["chuziren"]."</td><td>".$chuzilaiyuan["data"][$i]["chuzie"].
			"</td><td>".$chuzilaiyuan["data"][$i]["chuzibili"]."</td></tr>";
		}
		$html .= "<tr><th>总计</th><td>".$chuzilaiyuan["amount"]."</td><td>100%</td></tr>";
	}
	else {
		$html .= "<tr><td>出资来源</td><td colspan=\"3\"></td></tr>";
	}
	for($i=9; $i < count($fields); $i++) {
		if($role == "expert") {
			if($fields[$i]["engname"] == "lianxiren" || $fields[$i]["engname"] == "lianxidianhua" || $fields[$i]["engname"]== "pingshenyijian")
				continue;
		}
		$html .= "<tr><td>".$fields[$i]["chiname"]."</td><td colspan=\"3\">".$field_values[$fields[$i]["engname"]]."</td></tr>";
	}
	$html .= "</table>";
	if($field_values['file_name'] != "")
		$html .= '<h4>查看完整申报材料:</h4>'.l($field_values['file_name'], 'http://' .$_SERVER['HTTP_HOST'].'/sites/default/files/'.$field_values['url']).'<br />'.'<br />';
	return $html;
	
}
function state_traslation($state)
{
	$dict = array(
		0=>"正在修改",
		1=>"待审批",
		2=>"未通过初审",
		3=>"等待专家评审",
		4=>"未通过专家评审",
		5=>"等待尽调机构调查",
		6=>"未通过尽职调查",
		7=>"等待理事会决策",
		8=>"未通过理事会决策",
		9=>"进入过会企业公告",
		10=>"未通过过会企业公告",
		11=>"进入协议谈判",
		12=>"未通过协议谈判",
		13=>"基金已成立",
	);
	return $dict[$state];
}
function command_link($state,$id)
{
	if($state == 0) {
		return l("查看","enterprise_information2/viewbyid/".$id);
	}elseif($state == 1) {
		return l("审查","enterprise_information2/examine/".$id);
	}
}
function enterprise_information2_list_page()
{
	$html = "";
	$header = array( "拟设基金名称", "申报时间", "基金规模(万元)", "主投领域", "管理机构", "联系人", "联系电话","状态","操作");
	$query = db_select('nishejijin', 'n')
	->extend('PagerDefault')
	->limit(10)
	->extend('TableSort')
	->fields('n', array ("id","state","timestamp","nishejijinmingcheng","jijinguimo","zhuyaotouzilingyu","guanlijigoumingcheng","lianxiren","lianxidianhua"))
    ->condition('state', 2, '<');
	$results = $query->execute();
	$rows = array();
	foreach ($results as $node) {
		if(!in_array($node->state, array(0,1)))
			continue;
		$rows[] = array(
		'data' => array(
		$node->nishejijinmingcheng,
		date("Y年m月d日",$node->timestamp),
        (int)$node->jijinguimo,
		$node->zhuyaotouzilingyu,
		$node->guanlijigoumingcheng,
		$node->lianxiren,
		$node->lianxidianhua,
		state_traslation($node->state),
		command_link($node->state, $node->id),
		)
		);
	}
	$html .= theme('table',
	array(
	'header' => $header,
	'rows'=>$rows,
	'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
	'empty' => '没有记录',//Optional empty text for the table if resultset is empty
	)
	);
	$html .= theme('pager',
	array(
	'tags' => array()
	)
	);
	return ($html);
}
function enterprise_information2_history_list2_page()
{
	return enterprise_information2_history_list(2);
}
function enterprise_information2_history_list4_page()
{
	return enterprise_information2_history_list(4);
}
function enterprise_information2_history_list6_page()
{
	return enterprise_information2_history_list(6);
}
function enterprise_information2_history_list8_page()
{
	return enterprise_information2_history_list(8);
}
function enterprise_information2_history_list10_page()
{
	return enterprise_information2_history_list(10);
}
function enterprise_information2_history_list12_page()
{
	return enterprise_information2_history_list(12);
}
function enterprise_information2_history_list($state)
{
	$html = "";
	$header = array( "拟设基金名称", "申报时间", "基金规模(万元)", "主投领域", "管理机构", "联系人", "联系电话","状态","操作");
	$query = db_select('nishejijin', 'n')
	->extend('PagerDefault')
	->limit(10)
	->extend('TableSort')
	->fields('n', array("id","state","timestamp","nishejijinmingcheng","jijinguimo","zhuyaotouzilingyu","guanlijigoumingcheng","lianxiren","lianxidianhua"))
	->condition("state",$state);
	$results = $query->execute();
	$rows = array();
	foreach ($results as $node) {
		$rows[] = array(
		'data' => array(
		$node->nishejijinmingcheng,
		date("Y年m月d日",$node->timestamp),
        (int)$node->jijinguimo,
		$node->zhuyaotouzilingyu,
		$node->guanlijigoumingcheng,
		$node->lianxiren,
		$node->lianxidianhua,
		state_traslation($node->state),
		l("查看", 'enterprise_information2/viewbyid/'. $node->id),
		)
		);
	}
	$html .= theme('table',
	array(
	'header' => $header,
	'rows'=>$rows,
	'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
	'empty' => '没有记录',//Optional empty text for the table if resultset is empty
	)
	);
	$html .= theme('pager',
	array(
	'tags' => array()
	)
	);
	return ($html);
	
}
